package edu.itcs6112.za.service.jpa;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;

import edu.itcs6112.za.domain.Item;
import edu.itcs6112.za.repository.ItemRepository;
import edu.itcs6112.za.service.ItemService;


	@Service("itemService")
	@Repository
	@Transactional
	public class ItemServiceImpl implements ItemService {

		@Autowired
		private ItemRepository itemRepository;

		@Transactional(readOnly = true)
		public List<Item> findAll() {
			return Lists.newArrayList(itemRepository.findAll());
		}

		@Transactional(readOnly = true)
		public Item findById(Long id) {
			return itemRepository.findOne(id);
		}

		public Item save(Item item) {
			return itemRepository.save(item);
		}
		
		@Transactional(readOnly=true)
		public Page<Item> findAllByPage(Pageable pageable) {
			return itemRepository.findAll(pageable);
		}
		
		@Transactional(readOnly=true)
		public List<Item> findByType(String itemType) {
			return itemRepository.findByType(itemType);
		}
		
		@Transactional(readOnly=true)
		public List<Item> findBySearchString(String searchString) {
			return itemRepository.findBySearchString("%"+searchString+"%");
		}
	}
